home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume13 / rpc3.9 / part13 < prev    next >
Encoding:
Internet Message Format  |  1988-02-27  |  49.6 KB

  1. Subject:  v13i090:  Sun RPC, release 3.9, Part13/15
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Stephen X. Nahm <sxn@Sun.COM>
  7. Posting-number: Volume 13, Issue 90
  8. Archive-name: rpc3.9/part13
  9.  
  10. #! /bin/sh
  11. # This is a shell archive. To extract, remove the header and type "sh filename"
  12. #
  13. cd doc
  14. echo x - xdr.nts.ms
  15. sed -e 's/^X//' > xdr.nts.ms <<'Funky_Stuff'
  16. X.\" @(#)xdr.nts.ms    1.2 87/11/09 3.9 RPCSRC
  17. X.de BT
  18. X.if \\n%=1 .tl ''- % -''
  19. X..
  20. X.ND
  21. X.\" prevent excess underlining in nroff
  22. X.if n .fp 2 R
  23. X.OH 'eXternal Data Representation: Sun Technical Notes''Page %'
  24. X.EH 'Page %''eXternal Data Representation: Sun Technical Notes'
  25. X.if \\n%=1 .bp
  26. X.SH
  27. \&eXternal Data Representation: Sun Technical Notes
  28. X.IX XDR "Sun technical notes"
  29. X.LP
  30. This chapter contains technical notes on Sun's implementation of the
  31. eXternal Data Representation (XDR) standard, a set of library routines 
  32. that allow a C programmer to describe arbitrary data structures in a 
  33. machine-independent fashion.  For a formal specification of the XDR
  34. standard, see the
  35. \fIeXternal Data Representation Standard\fP\.
  36. XDR is the backbone of Sun's Remote Procedure Call package, in the 
  37. sense that data for remote procedure calls is transmitted using the 
  38. standard.  XDR library routines should be used to transmit data
  39. that is accessed (read or written) by more than one type of machine.\**
  40. X.FS
  41. X.IX XDR "system routines"
  42. For a compete specification of the system eXternal Data Representation
  43. routines, see the 
  44. X.I xdr (3N)
  45. manual page.
  46. X.FE
  47. X.LP
  48. This chapter contains a short tutorial overview of the XDR library 
  49. routines, a guide to accessing currently available XDR streams, and
  50. information on defining new streams and data types.  XDR was designed
  51. to work across different languages, operating systems, and machine 
  52. architectures.  Most users (particularly RPC users) will need only the
  53. information in sections 1, 2 and 3 of this document.
  54. Programmers wishing to implement RPC and XDR on new machines
  55. will need the information in the rest of this document, and especially the
  56. \fIeXternal Data Representation Standard\fP\.
  57. X.SH
  58. X.I
  59. NOTE:
  60. \fBrpcgen \fIcan be used to write XDR routines even in cases where no RPC
  61. calls are being made.
  62. X.LP
  63. On Sun systems,
  64. C programs that want to use XDR routines
  65. must include the file
  66. X.I <rpc/rpc.h> ,
  67. which contains all the necessary interfaces to the XDR system.
  68. Since the C library
  69. X.I libc.a
  70. contains all the XDR routines, compile as normal.
  71. X.DS
  72. % \fBcc\0\fIprogram\fP.c\fI
  73. X.DE
  74. X.
  75. X.ne 3i
  76. X.NH 0
  77. \&Justification
  78. X.IX "justification for XDR"
  79. X.IX "reason for XDR"
  80. X.LP
  81. Consider the following two programs,
  82. X.I writer
  83. X.ie t .DS
  84. X.el .DS L
  85. X.ft CW
  86. #include <stdio.h>
  87. X.sp.5
  88. main()            /* \fIwriter.c\fP */
  89. {
  90.     long i;
  91. X.sp.5
  92.     for (i = 0; i < 8; i++) {
  93.         if (fwrite((char *)&i, sizeof(i), 1, stdout) != 1) {
  94.             fprintf(stderr, "failed!\en");
  95.             exit(1);
  96.         }
  97.     }
  98. }
  99. X.DE
  100. and
  101. X.I reader
  102. X.ie t .DS
  103. X.el .DS L
  104. X.ft CW
  105. #include <stdio.h>
  106. X.sp.5
  107. main()            /* \fIreader.c\fP */
  108. {
  109.     long i, j;
  110. X.sp.5
  111.     for (j = 0; j < 8; j++) {
  112.         if (fread((char *)&i, sizeof (i), 1, stdin) != 1) {
  113.             fprintf(stderr, "failed!\en");
  114.             exit(1);
  115.         }
  116.         printf("%ld ", i);
  117.     }
  118.     printf("\en");
  119. }
  120. X.DE
  121. The two programs appear to be portable, because (a) they pass
  122. X.I lint
  123. checking, and (b) they exhibit the same behavior when executed
  124. on two different hardware architectures, a Sun and a VAX.
  125. X.LP
  126. Piping the output of the
  127. X.I writer 
  128. program to the
  129. X.I reader 
  130. program gives identical results on a Sun or a VAX.
  131. X.DS
  132. X.ft CW
  133. sun% writer | reader
  134. 0 1 2 3 4 5 6 7
  135. sun%
  136.  
  137. vax% writer | reader
  138. 0 1 2 3 4 5 6 7
  139. vax%
  140. X.DE
  141. With the advent of local area networks and 4.2BSD came the concept 
  142. of ``network pipes'' \(em a process produces data on one machine,
  143. and a second process consumes data on another machine.
  144. A network pipe can be constructed with
  145. X.I writer 
  146. and
  147. X.I reader .
  148. Here are the results if the first produces data on a Sun,
  149. and the second consumes data on a VAX.
  150. X.DS
  151. X.ft CW
  152. sun% writer | rsh vax reader
  153. 0 16777216 33554432 50331648 67108864 83886080 100663296
  154. 117440512
  155. sun%
  156. X.DE
  157. Identical results can be obtained by executing
  158. X.I writer 
  159. on the VAX and
  160. X.I reader 
  161. on the Sun.
  162. These results occur because the byte ordering
  163. of long integers differs between the VAX and the Sun,
  164. even though word size is the same.
  165. Note that $16777216$ is $2 sup 24$ \(em
  166. when four bytes are reversed, the 1 winds up in the 24th bit.
  167. X.LP
  168. Whenever data is shared by two or more machine types,
  169. there is a need for portable data.
  170. Programs can be made data-portable by replacing the
  171. X.I read
  172. and
  173. X.I write
  174. calls with calls to an XDR library routine
  175. X.I xdr_long
  176. a filter that knows the standard representation
  177. of a long integer in its external form.
  178. Here are the revised versions of
  179. X.I writer :
  180. X.ie t .DS
  181. X.el .DS L
  182. X.ft CW
  183. #include <stdio.h>
  184. #include <rpc/rpc.h>    /* \fIxdr is a sub-library of rpc\fP */
  185. X.sp.5
  186. main()        /* \fIwriter.c\fP */
  187. {
  188.     XDR xdrs;
  189.     long i;
  190. X.sp.5
  191.     xdrstdio_create(&xdrs, stdout, XDR_ENCODE);
  192.     for (i = 0; i < 8; i++) {
  193.         if (!xdr_long(&xdrs, &i)) {
  194.             fprintf(stderr, "failed!\en");
  195.             exit(1);
  196.         }
  197.     }
  198. }
  199. X.DE
  200. and
  201. X.I reader :
  202. X.ie t .DS
  203. X.el .DS L
  204. X.ft CW
  205. #include <stdio.h>
  206. #include <rpc/rpc.h>    /* \fIxdr is a sub-library of rpc\fP */
  207. X.sp.5
  208. main()        /* \fIreader.c\fP */
  209. {
  210.     XDR xdrs;
  211.     long i, j;
  212. X.sp.5
  213.     xdrstdio_create(&xdrs, stdin, XDR_DECODE);
  214.     for (j = 0; j < 8; j++) {
  215.         if (!xdr_long(&xdrs, &i)) {
  216.             fprintf(stderr, "failed!\en");
  217.             exit(1);
  218.         }
  219.         printf("%ld ", i);
  220.     }
  221.     printf("\en");
  222. }
  223. X.DE
  224. The new programs were executed on a Sun,
  225. on a VAX, and from a Sun to a VAX;
  226. the results are shown below.
  227. X.DS
  228. X.ft CW
  229. sun% writer | reader
  230. 0 1 2 3 4 5 6 7
  231. sun%
  232.  
  233. vax% writer | reader
  234. 0 1 2 3 4 5 6 7
  235. vax%
  236.  
  237. sun% writer | rsh vax reader
  238. 0 1 2 3 4 5 6 7
  239. sun%
  240. X.DE
  241. X.SH
  242. X.IX "portable data"
  243. X.I
  244. NOTE:
  245. Integers are just the tip of the portable-data iceberg.  Arbitrary
  246. data structures present portability problems, particularly with
  247. respect to alignment and pointers.  Alignment on word boundaries
  248. may cause the size of a structure to vary from machine to machine.
  249. And pointers, which are very convenient to use, have no meaning
  250. outside the machine where they are defined.\fP
  251. X.LP
  252. X.NH 1
  253. \&A Canonical Standard
  254. X.IX XDR "canonical standard"
  255. X.LP
  256. XDR's approach to standardizing data representations is 
  257. X.I canonical .
  258. That is, XDR defines a single byte order (Big Endian), a single
  259. floating-point representation (IEEE), and so on.  Any program running on
  260. any machine can use XDR to create portable data by translating its
  261. local representation to the XDR standard representations; similarly, any
  262. program running on any machine can read portable data by translating the
  263. XDR standard representaions to its local equivalents.  The single standard
  264. completely decouples programs that create or send portable data from those
  265. that use or receive portable data.  The advent of a new machine or a new
  266. language has no effect opn the community of existing portable data creators
  267. and users.  A new machine joins this community be being ``taught'' how to
  268. convert the standard representations and its local representations; the
  269. local representations of other machines are irrelevant.  Conversely, to
  270. existing programs running on other machines, the local representations of
  271. the new machine are also irrelevant; such programs can immediately read
  272. portable data produced by the new machine because such data conforms to the
  273. canonical standards that they already understand.
  274. X.LP
  275. There are strong precedents for XDR's canonical approach.  For example,
  276. TCP/IP, UDP/IP, XNS, Ethernet, and, indeed, all protocols below layer five
  277. of the ISO model, are canonical protocols.  The advantage of any canonical 
  278. approach is simplicity; in the case of XDR, a single set of conversion 
  279. routines is written once and is never touched again.  The canonical approach 
  280. has a disadvantage, but it is unimportant in real-world data transfer 
  281. applications.  Suppose two Little-Endian machines are transferring integers
  282. according to the XDR standard.  The sending machine converts the integers 
  283. from Little-Endian byte order to XDR (Big-Endian) byte order; the receiving
  284. machine performs the reverse conversion.  Because both machines observe the
  285. same byte order, their conversions are unnecessary.  The point, however, is
  286. not necessity, but cost as compared to the alternative.
  287. X.LP
  288. The time spent converting to and from a canonical representation is
  289. insignificant, especially in networking applications.  Most of the time 
  290. required to prepare a data structure for transfer is not spent in conversion 
  291. but in traversing the elements of the data structure.  To transmit a tree, 
  292. for example, each leaf must be visited and each element in a leaf record must
  293. be copied to a buffer and aligned there; storage for the leaf may have to be
  294. deallocated as well.  Similarly, to receive a tree, storage must be 
  295. allocated for each leaf, data must be moved from the buffer to the leaf and
  296. properly aligned, and pointers must be constructed to link the leaves 
  297. together.  Every machine pays the cost of traversing and copying data
  298. structures whether or not conversion is required.  In networking 
  299. applications, communications overhead\(emthe time required to move the data
  300. down through the sender's protocol layers, across the network and up through 
  301. the receiver's protocol layers\(emdwarfs conversion overhead.
  302. X.NH 1
  303. \&The XDR Library
  304. X.IX "library of XDR routines"
  305. X.IX "XDR" "library"
  306. X.LP
  307. The XDR library not only solves data portability problems, it also
  308. allows you to write and read arbitrary C constructs in a consistent, 
  309. specified, well-documented manner.  Thus, it can make sense to use the 
  310. library even when the data is not shared among machines on a network.
  311. X.LP
  312. The XDR library has filter routines for
  313. strings (null-terminated arrays of bytes),
  314. structures, unions, and arrays, to name a few.
  315. Using more primitive routines,
  316. you can write your own specific XDR routines
  317. to describe arbitrary data structures,
  318. including elements of arrays, arms of unions,
  319. or objects pointed at from other structures.
  320. The structures themselves may contain arrays of arbitrary elements,
  321. or pointers to other structures.
  322. X.LP
  323. Let's examine the two programs more closely.
  324. There is a family of XDR stream creation routines
  325. in which each member treats the stream of bits differently.
  326. In our example, data is manipulated using standard I/O routines,
  327. so we use
  328. X.I xdrstdio_create.
  329. The parameters to XDR stream creation routines
  330. vary according to their function.
  331. In our example,
  332. X.I xdrstdio_create 
  333. takes a pointer to an XDR structure that it initializes,
  334. a pointer to a
  335. X.I FILE 
  336. that the input or output is performed on, and the operation.
  337. The operation may be
  338. X.I XDR_ENCODE
  339. for serializing in the
  340. X.I writer 
  341. program, or
  342. X.I XDR_DECODE
  343. for deserializing in the
  344. X.I reader 
  345. program.
  346. X.LP
  347. Note: RPC users never need to create XDR streams;
  348. the RPC system itself creates these streams,
  349. which are then passed to the users.
  350. X.LP
  351. The
  352. X.I xdr_long
  353. primitive is characteristic of most XDR library 
  354. primitives and all client XDR routines.
  355. First, the routine returns
  356. X.I FALSE 
  357. (0) if it fails, and
  358. X.I TRUE 
  359. (1) if it succeeds.
  360. Second, for each data type,
  361. X.I xxx ,
  362. there is an associated XDR routine of the form:
  363. X.DS
  364. X.ft CW
  365. xdr_xxx(xdrs, xp)
  366.     XDR *xdrs;
  367.     xxx *xp;
  368. {
  369. }
  370. X.DE
  371. In our case,
  372. X.I xxx 
  373. is long, and the corresponding XDR routine is
  374. a primitive,
  375. X.I xdr_long
  376. The client could also define an arbitrary structure
  377. X.I xxx 
  378. in which case the client would also supply the routine
  379. X.I xdr_xxx ,
  380. describing each field by calling XDR routines
  381. of the appropriate type.
  382. In all cases the first parameter,
  383. X.I xdrs 
  384. can be treated as an opaque handle,
  385. and passed to the primitive routines.
  386. X.LP
  387. XDR routines are direction independent;
  388. that is, the same routines are called to serialize or deserialize data.
  389. This feature is critical to software engineering of portable data.
  390. The idea is to call the same routine for either operation \(em
  391. this almost guarantees that serialized data can also be deserialized.
  392. One routine is used by both producer and consumer of networked data.
  393. This is implemented by always passing the address
  394. of an object rather than the object itself \(em
  395. only in the case of deserialization is the object modified.
  396. This feature is not shown in our trivial example,
  397. but its value becomes obvious when nontrivial data structures
  398. are passed among machines.  If needed, the user can obtain the 
  399. direction of the XDR operation.  See the
  400. \fIXDR Operation Directions\fP
  401. section of this chapter for details.
  402. X.LP
  403. Let's look at a slightly more complicated example.
  404. Assume that a person's gross assets and liabilities
  405. are to be exchanged among processes.
  406. Also assume that these values are important enough
  407. to warrant their own data type:
  408. X.ie t .DS
  409. X.el .DS L
  410. X.ft CW
  411. struct gnumbers {
  412.     long g_assets;
  413.     long g_liabilities;
  414. };
  415. X.DE
  416. The corresponding XDR routine describing this structure would be:
  417. X.ie t .DS
  418. X.el .DS L
  419. X.ft CW
  420. bool_t          /* \fITRUE is success, FALSE is failure\fP */
  421. xdr_gnumbers(xdrs, gp)
  422.     XDR *xdrs;
  423.     struct gnumbers *gp;
  424. {
  425.     if (xdr_long(xdrs, &gp->g_assets) &&
  426.         xdr_long(xdrs, &gp->g_liabilities))
  427.         return(TRUE);
  428.     return(FALSE);
  429. }
  430. X.DE
  431. Note that the parameter
  432. X.I xdrs 
  433. is never inspected or modified;
  434. it is only passed on to the subcomponent routines.
  435. It is imperative to inspect the return value of each XDR routine call,
  436. and to give up immediately and return
  437. X.I FALSE 
  438. if the subroutine fails.
  439. X.LP
  440. This example also shows that the type
  441. X.I bool_t
  442. is declared as an integer whose only values are
  443. X.I TRUE 
  444. (1) and
  445. X.I FALSE 
  446. (0).  This document uses the following definitions:
  447. X.ie t .DS
  448. X.el .DS L
  449. X.ft CW
  450. #define bool_t    int
  451. #define TRUE    1
  452. #define FALSE    0
  453. X.sp.5
  454. #define enum_t int    /* \fIenum_t used for generic enums\fP */
  455. X.DE
  456. X.LP
  457. Keeping these conventions in mind,
  458. X.I xdr_gnumbers
  459. can be rewritten as follows:
  460. X.ie t .DS
  461. X.el .DS L
  462. X.ft CW
  463. xdr_gnumbers(xdrs, gp)
  464.     XDR *xdrs;
  465.     struct gnumbers *gp;
  466. {
  467.     return(xdr_long(xdrs, &gp->g_assets) &&
  468.         xdr_long(xdrs, &gp->g_liabilities));
  469. }
  470. X.DE
  471. This document uses both coding styles.
  472. X.
  473. X.NH 1
  474. \&XDR Library Primitives
  475. X.IX "library primitives for XDR"
  476. X.IX "XDR "library primitives"
  477. X.LP
  478. This section gives a synopsis of each XDR primitive.
  479. It starts with basic data types and moves on to constructed data types.
  480. Finally, XDR utilities are discussed.
  481. The interface to these primitives
  482. and utilities is defined in the include file
  483. X.I <rpc/xdr.h> ,
  484. automatically included by
  485. X.I <rpc/rpc.h> .
  486. X.
  487. X.NH 2
  488. \&Number Filters
  489. X.IX "XDR library" "number filters"
  490. X.LP
  491. The XDR library provides primitives to translate between numbers
  492. and their corresponding external representations.
  493. Primitives cover the set of numbers in:
  494. X.EQ
  495. [signed, unsigned] * [short, int, long]
  496. X.EN
  497. Specifically, the eight primitives are:
  498. X.DS
  499. X.ft CW
  500. bool_t xdr_char(xdrs, cp)
  501.     XDR *xdrs;
  502.     char *cp;
  503. X.sp.5
  504. bool_t xdr_u_char(xdrs, ucp)
  505.     XDR *xdrs;
  506.     unsigned char *ucp;
  507. X.sp.5
  508. bool_t xdr_int(xdrs, ip)
  509.     XDR *xdrs;
  510.     int *ip;
  511. X.sp.5
  512. bool_t xdr_u_int(xdrs, up)
  513.     XDR *xdrs;
  514.     unsigned *up;
  515. X.sp.5
  516. bool_t xdr_long(xdrs, lip)
  517.     XDR *xdrs;
  518.     long *lip;
  519. X.sp.5
  520. bool_t xdr_u_long(xdrs, lup)
  521.     XDR *xdrs;
  522.     u_long *lup;
  523. X.sp.5
  524. bool_t xdr_short(xdrs, sip)
  525.     XDR *xdrs;
  526.     short *sip;
  527. X.sp.5
  528. bool_t xdr_u_short(xdrs, sup)
  529.     XDR *xdrs;
  530.     u_short *sup;
  531. X.DE
  532. The first parameter,
  533. X.I xdrs ,
  534. is an XDR stream handle.
  535. The second parameter is the address of the number
  536. that provides data to the stream or receives data from it.
  537. All routines return
  538. X.I TRUE 
  539. if they complete successfully, and
  540. X.I FALSE 
  541. otherwise.
  542. X.
  543. X.NH 2
  544. \&Floating Point Filters
  545. X.IX "XDR library" "floating point filters"
  546. X.LP
  547. The XDR library also provides primitive routines
  548. for C's floating point types:
  549. X.DS
  550. X.ft CW
  551. bool_t xdr_float(xdrs, fp)
  552.     XDR *xdrs;
  553.     float *fp;
  554. X.sp.5
  555. bool_t xdr_double(xdrs, dp)
  556.     XDR *xdrs;
  557.     double *dp;
  558. X.DE
  559. The first parameter,
  560. X.I xdrs 
  561. is an XDR stream handle.
  562. The second parameter is the address
  563. of the floating point number that provides data to the stream
  564. or receives data from it.
  565. All routines return
  566. X.I TRUE 
  567. if they complete successfully, and
  568. X.I FALSE 
  569. otherwise.
  570. X.LP
  571. Note: Since the numbers are represented in IEEE floating point,
  572. routines may fail when decoding a valid IEEE representation
  573. into a machine-specific representation, or vice-versa.
  574. X.
  575. X.NH 2
  576. \&Enumeration Filters
  577. X.IX "XDR library" "enumeration filters"
  578. X.LP
  579. The XDR library provides a primitive for generic enumerations.
  580. The primitive assumes that a C
  581. X.I enum 
  582. has the same representation inside the machine as a C integer.
  583. The boolean type is an important instance of the
  584. X.I enum .
  585. The external representation of a boolean is always
  586. X.I TRUE 
  587. (1) or 
  588. X.I FALSE 
  589. (0).
  590. X.DS
  591. X.ft CW
  592. #define bool_t    int
  593. #define FALSE    0
  594. #define TRUE    1
  595. X.sp.5
  596. #define enum_t int
  597. X.sp.5
  598. bool_t xdr_enum(xdrs, ep)
  599.     XDR *xdrs;
  600.     enum_t *ep;
  601. X.sp.5
  602. bool_t xdr_bool(xdrs, bp)
  603.     XDR *xdrs;
  604.     bool_t *bp;
  605. X.DE
  606. The second parameters
  607. X.I ep
  608. and
  609. X.I bp
  610. are addresses of the associated type
  611. that provides data to, or receives data from, the stream
  612. X.I xdrs
  613. The routine returns
  614. X.I FALSE 
  615. if the number of characters exceeds
  616. X.I maxlength ,
  617. and
  618. X.I TRUE 
  619. if it doesn't.
  620. X.
  621. X.NH 2
  622. \&No Data
  623. X.IX "XDR library" "no data"
  624. X.LP
  625. Occasionally, an XDR routine must be supplied to the RPC system,
  626. even when no data is passed or required.
  627. The library provides such a routine:
  628. X.DS
  629. X.ft CW
  630. bool_t xdr_void();  /* \fIalways returns TRUE\fP */
  631. X.DE
  632. X.
  633. X.NH 2
  634. \&Constructed Data Type Filters
  635. X.IX "XDR library" "constructed data type filters"
  636. X.LP
  637. Constructed or compound data type primitives
  638. require more parameters and perform more complicated functions
  639. then the primitives discussed above.
  640. This section includes primitives for
  641. strings, arrays, unions, and pointers to structures.
  642. X.LP
  643. Constructed data type primitives may use memory management.
  644. In many cases, memory is allocated when deserializing data with
  645. X.I XDR_DECODE
  646. Therefore, the XDR package must provide means to deallocate memory.
  647. This is done by an XDR operation,
  648. X.I XDR_FREE
  649. To review, the three XDR directional operations are
  650. X.I XDR_ENCODE ,
  651. X.I XDR_DECODE
  652. and
  653. X.I XDR_FREE .
  654. X.
  655. X.NH 3
  656. \&Strings
  657. X.IX "XDR library" "strings"
  658. X.IX "strings"
  659. X.LP
  660. In C, a string is defined as a sequence of bytes
  661. terminated by a null byte,
  662. which is not considered when calculating string length.
  663. However, when a string is passed or manipulated,
  664. a pointer to it is employed.
  665. Therefore, the XDR library defines a string to be a
  666. X.I "char
  667. and not a sequence of characters.
  668. The external representation of a string is drastically different
  669. from its internal representation.
  670. Externally, strings are represented as
  671. sequences of ASCII characters,
  672. while internally, they are represented with character pointers.
  673. Conversion between the two representations
  674. is accomplished with the routine
  675. X.I xdr_string.
  676. X.DS
  677. X.ft CW
  678. bool_t xdr_string(xdrs, sp, maxlength)
  679.     XDR *xdrs;
  680.     char **sp;
  681.     u_int maxlength;
  682. X.DE
  683. The first parameter
  684. X.I xdrs 
  685. is the XDR stream handle.
  686. The second parameter
  687. X.I sp 
  688. is a pointer to a string (type
  689. X.I "char
  690. The third parameter
  691. X.I maxlength 
  692. specifies the maximum number of bytes allowed during encoding or decoding;
  693. its value is usually specified by a protocol.
  694. For example, a protocol specification may say
  695. that a file name may be no longer than 255 characters.
  696. The routine returns
  697. X.I FALSE 
  698. if the number of characters exceeds
  699. X.I maxlength ,
  700. and
  701. X.I TRUE 
  702. if it doesn't.
  703. X.LP
  704. The behavior of
  705. X.I xdr_string
  706. is similar to the behavior of other routines
  707. discussed in this section.  The direction
  708. X.I XDR_ENCODE 
  709. is easiest to understand.  The parameter
  710. X.I sp 
  711. points to a string of a certain length;
  712. if the string does not exceed
  713. X.I maxlength ,
  714. the bytes are serialized.
  715. X.LP
  716. The effect of deserializing a string is subtle.
  717. First the length of the incoming string is determined;
  718. it must not exceed
  719. X.I maxlength .
  720. Next
  721. X.I sp 
  722. is dereferenced; if the the value is
  723. X.I NULL ,
  724. then a string of the appropriate length is allocated and
  725. X.I *sp 
  726. is set to this string.
  727. If the original value of
  728. X.I *sp 
  729. is non-null, then the XDR package assumes
  730. that a target area has been allocated,
  731. which can hold strings no longer than
  732. X.I maxlength .
  733. In either case, the string is decoded into the target area.
  734. The routine then appends a null character to the string.
  735. X.LP
  736. In the
  737. X.I XDR_FREE 
  738. operation, the string is obtained by dereferencing
  739. X.I sp .
  740. If the string is not
  741. X.I NULL ,
  742. it is freed and
  743. X.I *sp 
  744. is set to
  745. X.I NULL .
  746. In this operation,
  747. X.I xdr_string 
  748. ignores the
  749. X.I maxlength 
  750. parameter.
  751. X.
  752. X.NH 3
  753. \&Byte Arrays
  754. X.IX "XDR library" "byte arrays"
  755. X.IX "byte arrays"
  756. X.LP
  757. Often variable-length arrays of bytes are preferable to strings.
  758. Byte arrays differ from strings in the following three ways: 
  759. 1) the length of the array (the byte count) is explicitly
  760. located in an unsigned integer,
  761. 2) the byte sequence is not terminated by a null character, and
  762. 3) the external representation of the bytes is the same as their
  763. internal representation.
  764. The primitive
  765. X.I xdr_bytes
  766. converts between the internal and external
  767. representations of byte arrays:
  768. X.DS
  769. X.ft CW
  770. bool_t xdr_bytes(xdrs, bpp, lp, maxlength)
  771.     XDR *xdrs;
  772.     char **bpp;
  773.     u_int *lp;
  774.     u_int maxlength;
  775. X.DE
  776. The usage of the first, second and fourth parameters
  777. are identical to the first, second and third parameters of
  778. X.I xdr_string ,
  779. respectively.
  780. The length of the byte area is obtained by dereferencing
  781. X.I lp 
  782. when serializing;
  783. X.I *lp 
  784. is set to the byte length when deserializing.
  785. X.
  786. X.NH 3
  787. \&Arrays
  788. X.IX "XDR library" "arrays"
  789. X.IX "arrays"
  790. X.LP
  791. The XDR library package provides a primitive
  792. for handling arrays of arbitrary elements.
  793. The
  794. X.I xdr_bytes
  795. routine treats a subset of generic arrays,
  796. in which the size of array elements is known to be 1,
  797. and the external description of each element is built-in.
  798. The generic array primitive,
  799. X.I xdr_array
  800. requires parameters identical to those of
  801. X.I xdr_bytes
  802. plus two more:
  803. the size of array elements,
  804. and an XDR routine to handle each of the elements.
  805. This routine is called to encode or decode
  806. each element of the array.
  807. X.DS
  808. X.ft CW
  809. bool_t
  810. xdr_array(xdrs, ap, lp, maxlength, elementsiz, xdr_element)
  811.     XDR *xdrs;
  812.     char **ap;
  813.     u_int *lp;
  814.     u_int maxlength;
  815.     u_int elementsiz;
  816.     bool_t (*xdr_element)();
  817. X.DE
  818. The parameter
  819. X.I ap 
  820. is the address of the pointer to the array.
  821. If
  822. X.I *ap 
  823. is
  824. X.I NULL 
  825. when the array is being deserialized,
  826. XDR allocates an array of the appropriate size and sets
  827. X.I *ap 
  828. to that array.
  829. The element count of the array is obtained from
  830. X.I *lp 
  831. when the array is serialized;
  832. X.I *lp 
  833. is set to the array length when the array is deserialized. 
  834. The parameter
  835. X.I maxlength 
  836. is the maximum number of elements that the array is allowed to have;
  837. X.I elementsiz
  838. is the byte size of each element of the array
  839. (the C function
  840. X.I sizeof
  841. can be used to obtain this value).
  842. The routine
  843. X.I xdr_element
  844. is called to serialize, deserialize, or free
  845. each element of the array.
  846. X.
  847. X.LP
  848. Before defining more constructed data types, it is appropriate to 
  849. present three examples.
  850. X.LP
  851. X.I "Example A:"
  852. X.br
  853. A user on a networked machine can be identified by 
  854. (a) the machine name, such as
  855. X.I krypton :
  856. see the
  857. X.I gethostname 
  858. man page; (b) the user's UID: see the
  859. X.I geteuid 
  860. man page; and (c) the group numbers to which the user belongs: 
  861. see the
  862. X.I getgroups 
  863. man page.  A structure with this information and its associated 
  864. XDR routine could be coded like this:
  865. X.ie t .DS
  866. X.el .DS L
  867. X.ft CW
  868. struct netuser {
  869.     char    *nu_machinename;
  870.     int     nu_uid;
  871.     u_int   nu_glen;
  872.     int     *nu_gids;
  873. };
  874. #define NLEN 255    /* \fImachine names < 256 chars\fP */
  875. #define NGRPS 20    /* \fIuser can't be in > 20 groups\fP */
  876. X.sp.5
  877. bool_t
  878. xdr_netuser(xdrs, nup)
  879.     XDR *xdrs;
  880.     struct netuser *nup;
  881. {
  882.     return(xdr_string(xdrs, &nup->nu_machinename, NLEN) &&
  883.         xdr_int(xdrs, &nup->nu_uid) &&
  884.         xdr_array(xdrs, &nup->nu_gids, &nup->nu_glen, 
  885.         NGRPS, sizeof (int), xdr_int));
  886. }
  887. X.DE
  888. X.LP
  889. X.I "Example B:"
  890. X.br
  891. A party of network users could be implemented
  892. as an array of
  893. X.I netuser
  894. structure.
  895. The declaration and its associated XDR routines
  896. are as follows:
  897. X.ie t .DS
  898. X.el .DS L
  899. X.ft CW
  900. struct party {
  901.     u_int p_len;
  902.     struct netuser *p_nusers;
  903. };
  904. #define PLEN 500    /* \fImax number of users in a party\fP */
  905. X.sp.5
  906. bool_t
  907. xdr_party(xdrs, pp)
  908.     XDR *xdrs;
  909.     struct party *pp;
  910. {
  911.     return(xdr_array(xdrs, &pp->p_nusers, &pp->p_len, PLEN,
  912.         sizeof (struct netuser), xdr_netuser));
  913. }
  914. X.DE
  915. X.LP
  916. X.I "Example C:"
  917. X.br
  918. The well-known parameters to
  919. X.I main ,
  920. X.I argc
  921. and
  922. X.I argv
  923. can be combined into a structure.
  924. An array of these structures can make up a history of commands.
  925. The declarations and XDR routines might look like:
  926. X.ie t .DS
  927. X.el .DS L
  928. X.ft CW
  929. struct cmd {
  930.     u_int c_argc;
  931.     char **c_argv;
  932. };
  933. #define ALEN 1000   /* \fIargs cannot be > 1000 chars\fP */
  934. #define NARGC 100   /* \fIcommands cannot have > 100 args\fP */
  935.  
  936. struct history {
  937.     u_int h_len;
  938.     struct cmd *h_cmds;
  939. };
  940. #define NCMDS 75    /* \fIhistory is no more than 75 commands\fP */
  941.  
  942. bool_t
  943. xdr_wrap_string(xdrs, sp)
  944.     XDR *xdrs;
  945.     char **sp;
  946. {
  947.     return(xdr_string(xdrs, sp, ALEN));
  948. }
  949. X.DE
  950. X.ie t .DS
  951. X.el .DS L
  952. X.ft CW
  953. bool_t
  954. xdr_cmd(xdrs, cp)
  955.     XDR *xdrs;
  956.     struct cmd *cp;
  957. {
  958.     return(xdr_array(xdrs, &cp->c_argv, &cp->c_argc, NARGC,
  959.         sizeof (char *), xdr_wrap_string));
  960. }
  961. X.DE
  962. X.ie t .DS
  963. X.el .DS L
  964. X.ft CW
  965. bool_t
  966. xdr_history(xdrs, hp)
  967.     XDR *xdrs;
  968.     struct history *hp;
  969. {
  970.     return(xdr_array(xdrs, &hp->h_cmds, &hp->h_len, NCMDS,
  971.         sizeof (struct cmd), xdr_cmd));
  972. }
  973. X.DE
  974. The most confusing part of this example is that the routine
  975. X.I xdr_wrap_string 
  976. is needed to package the
  977. X.I xdr_string 
  978. routine, because the implementation of
  979. X.I xdr_array 
  980. only passes two parameters to the array element description routine;
  981. X.I xdr_wrap_string 
  982. supplies the third parameter to
  983. X.I xdr_string .
  984. X.LP
  985. By now the recursive nature of the XDR library should be obvious.
  986. Let's continue with more constructed data types.
  987. X.
  988. X.NH 3
  989. \&Opaque Data
  990. X.IX "XDR library" "opaque data"
  991. X.IX "opaque data"
  992. X.LP
  993. In some protocols, handles are passed from a server to client.
  994. The client passes the handle back to the server at some later time.
  995. Handles are never inspected by clients;
  996. they are obtained and submitted.
  997. That is to say, handles are opaque.
  998. The primitive
  999. X.I xdr_opaque
  1000. is used for describing fixed sized, opaque bytes.
  1001. X.DS
  1002. X.ft CW
  1003. bool_t xdr_opaque(xdrs, p, len)
  1004.     XDR *xdrs;
  1005.     char *p;
  1006.     u_int len;
  1007. X.DE
  1008. The parameter
  1009. X.I p 
  1010. is the location of the bytes;
  1011. X.I len
  1012. is the number of bytes in the opaque object.
  1013. By definition, the actual data
  1014. contained in the opaque object are not machine portable.
  1015. X.
  1016. X.NH 3
  1017. \&Fixed Sized Arrays
  1018. X.IX "XDR library" "fixed sized arrays"
  1019. X.IX "fixed sized arrays"
  1020. X.LP
  1021. The XDR library provides a primitive,
  1022. X.I xdr_vector ,
  1023. for fixed-length arrays.
  1024. X.ie t .DS
  1025. X.el .DS L
  1026. X.ft CW
  1027. #define NLEN 255    /* \fImachine names must be < 256 chars\fP */
  1028. #define NGRPS 20    /* \fIuser belongs to exactly 20 groups\fP */
  1029. X.sp.5
  1030. struct netuser {
  1031.     char *nu_machinename;
  1032.     int nu_uid;
  1033.     int nu_gids[NGRPS];
  1034. };
  1035. X.sp.5
  1036. bool_t
  1037. xdr_netuser(xdrs, nup)
  1038.     XDR *xdrs;
  1039.     struct netuser *nup;
  1040. {
  1041.     int i;
  1042. X.sp.5
  1043.     if (!xdr_string(xdrs, &nup->nu_machinename, NLEN))
  1044.         return(FALSE);
  1045.     if (!xdr_int(xdrs, &nup->nu_uid))
  1046.         return(FALSE);
  1047.     if (!xdr_vector(xdrs, nup->nu_gids, NGRPS, sizeof(int), 
  1048.         xdr_int)) {
  1049.             return(FALSE);
  1050.     }
  1051.     return(TRUE);
  1052. }
  1053. X.DE
  1054. X.
  1055. X.NH 3
  1056. \&Discriminated Unions
  1057. X.IX "XDR library" "discriminated unions"
  1058. X.IX "discriminated unions"
  1059. X.LP
  1060. The XDR library supports discriminated unions.
  1061. A discriminated union is a C union and an
  1062. X.I enum_t
  1063. value that selects an ``arm'' of the union.
  1064. X.DS
  1065. X.ft CW
  1066. struct xdr_discrim {
  1067.     enum_t value;
  1068.     bool_t (*proc)();
  1069. };
  1070. X.sp.5
  1071. bool_t xdr_union(xdrs, dscmp, unp, arms, defaultarm)
  1072.     XDR *xdrs;
  1073.     enum_t *dscmp;
  1074.     char *unp;
  1075.     struct xdr_discrim *arms;
  1076.     bool_t (*defaultarm)();  /* \fImay equal NULL\fP */
  1077. X.DE
  1078. First the routine translates the discriminant of the union located at 
  1079. X.I *dscmp .
  1080. The discriminant is always an
  1081. X.I enum_t
  1082. Next the union located at
  1083. X.I *unp 
  1084. is translated.
  1085. The parameter
  1086. X.I arms
  1087. is a pointer to an array of
  1088. X.I xdr_discrim
  1089. structures. 
  1090. Each structure contains an order pair of
  1091. X.I [value,proc] .
  1092. If the union's discriminant is equal to the associated
  1093. X.I value
  1094. then the
  1095. X.I proc
  1096. is called to translate the union.
  1097. The end of the
  1098. X.I xdr_discrim
  1099. structure array is denoted by a routine of value
  1100. X.I NULL 
  1101. (0).  If the discriminant is not found in the
  1102. X.I arms
  1103. array, then the
  1104. X.I defaultarm
  1105. procedure is called if it is non-null;
  1106. otherwise the routine returns
  1107. X.I FALSE .
  1108. X.LP
  1109. X.I "Example D:"
  1110. Suppose the type of a union may be integer,
  1111. character pointer (a string), or a
  1112. X.I gnumbers 
  1113. structure.
  1114. Also, assume the union and its current type
  1115. are declared in a structure.
  1116. The declaration is:
  1117. X.ie t .DS
  1118. X.el .DS L
  1119. X.ft CW
  1120. enum utype { INTEGER=1, STRING=2, GNUMBERS=3 };
  1121. X.sp.5
  1122. struct u_tag {
  1123.     enum utype utype;   /* \fIthe union's discriminant\fP */
  1124.     union {
  1125.         int ival;
  1126.         char *pval;
  1127.         struct gnumbers gn;
  1128.     } uval;
  1129. };
  1130. X.DE
  1131. The following constructs and XDR procedure (de)serialize
  1132. the discriminated union:
  1133. X.ie t .DS
  1134. X.el .DS L
  1135. X.ft CW
  1136. struct xdr_discrim u_tag_arms[4] = {
  1137.     { INTEGER, xdr_int },
  1138.     { GNUMBERS, xdr_gnumbers }
  1139.     { STRING, xdr_wrap_string },
  1140.     { __dontcare__, NULL }
  1141.     /* \fIalways terminate arms with a NULL xdr_proc\fP */
  1142. }
  1143. X.sp.5
  1144. bool_t
  1145. xdr_u_tag(xdrs, utp)
  1146.     XDR *xdrs;
  1147.     struct u_tag *utp;
  1148. {
  1149.     return(xdr_union(xdrs, &utp->utype, &utp->uval,
  1150.         u_tag_arms, NULL));
  1151. }
  1152. X.DE
  1153. The routine
  1154. X.I xdr_gnumbers 
  1155. was presented above in the 
  1156. \fIThe XDR Library\fP
  1157. section.
  1158. X.I xdr_wrap_string 
  1159. was presented in example C.
  1160. The default 
  1161. X.I arm 
  1162. parameter to
  1163. X.I xdr_union 
  1164. (the last parameter) is
  1165. X.I NULL 
  1166. in this example.  Therefore the value of the union's discriminant
  1167. may legally take on only values listed in the
  1168. X.I u_tag_arms 
  1169. array.  This example also demonstrates that
  1170. the elements of the arm's array do not need to be sorted.
  1171. X.LP
  1172. It is worth pointing out that the values of the discriminant
  1173. may be sparse, though in this example they are not.
  1174. It is always good
  1175. practice to assign explicitly integer values to each element of the
  1176. discriminant's type.
  1177. This practice both documents the external
  1178. representation of the discriminant and guarantees that different
  1179. C compilers emit identical discriminant values.
  1180. X.LP
  1181. Exercise: Implement
  1182. X.I xdr_union 
  1183. using the other primitives in this section.
  1184. X.
  1185. X.NH 3
  1186. \&Pointers
  1187. X.IX "XDR library" "pointers"
  1188. X.IX "pointers"
  1189. X.LP
  1190. In C it is often convenient to put pointers
  1191. to another structure within a structure.
  1192. The primitive
  1193. X.I xdr_reference
  1194. makes it easy to serialize, deserialize, and free
  1195. these referenced structures.
  1196. X.DS
  1197. X.ft CW
  1198. bool_t xdr_reference(xdrs, pp, size, proc)
  1199.     XDR *xdrs;
  1200.     char **pp;
  1201.     u_int ssize;
  1202.     bool_t (*proc)();
  1203. X.DE
  1204. X.LP
  1205. Parameter
  1206. X.I pp 
  1207. is the address of
  1208. the pointer to the structure;
  1209. parameter
  1210. X.I ssize
  1211. is the size in bytes of the structure
  1212. (use the C function
  1213. X.I sizeof
  1214. to obtain this value); and
  1215. X.I proc
  1216. is the XDR routine that describes the structure.
  1217. When decoding data, storage is allocated if
  1218. X.I *pp 
  1219. is
  1220. X.I NULL .
  1221. X.LP
  1222. There is no need for a primitive
  1223. X.I xdr_struct
  1224. to describe structures within structures,
  1225. because pointers are always sufficient.
  1226. X.LP
  1227. Exercise: Implement
  1228. X.I xdr_reference 
  1229. using
  1230. X.I xdr_array .
  1231. Warning:
  1232. X.I xdr_reference 
  1233. and
  1234. X.I xdr_array 
  1235. are NOT interchangeable external representations of data.
  1236. X.LP
  1237. X.I "Example E:"
  1238. Suppose there is a structure containing a person's name
  1239. and a pointer to a
  1240. X.I gnumbers 
  1241. structure containing the person's gross assets and liabilities.
  1242. The construct is:
  1243. X.DS
  1244. X.ft CW
  1245. struct pgn {
  1246.     char *name;
  1247.     struct gnumbers *gnp;
  1248. };
  1249. X.DE
  1250. The corresponding XDR routine for this structure is:
  1251. X.DS
  1252. X.ft CW
  1253. bool_t
  1254. xdr_pgn(xdrs, pp)
  1255.     XDR *xdrs;
  1256.     struct pgn *pp;
  1257. {
  1258.     if (xdr_string(xdrs, &pp->name, NLEN) &&
  1259.       xdr_reference(xdrs, &pp->gnp,
  1260.       sizeof(struct gnumbers), xdr_gnumbers))
  1261.         return(TRUE);
  1262.     return(FALSE);
  1263. }
  1264. X.DE
  1265. X.
  1266. X.IX "pointer semantics and XDR"
  1267. X.I "Pointer Semantics and XDR" 
  1268. X.LP
  1269. In many applications, C programmers attach double meaning to 
  1270. the values of a pointer.  Typically the value
  1271. X.I NULL 
  1272. (or zero) means data is not needed,
  1273. yet some application-specific interpretation applies.
  1274. In essence, the C programmer is encoding
  1275. a discriminated union efficiently
  1276. by overloading the interpretation of the value of a pointer.
  1277. For instance, in example E a
  1278. X.I NULL 
  1279. pointer value for
  1280. X.I gnp
  1281. could indicate that
  1282. the person's assets and liabilities are unknown.
  1283. That is, the pointer value encodes two things:
  1284. whether or not the data is known;
  1285. and if it is known, where it is located in memory.
  1286. Linked lists are an extreme example of the use
  1287. of application-specific pointer interpretation.
  1288. X.LP
  1289. The primitive
  1290. X.I xdr_reference
  1291. cannot and does not attach any special
  1292. meaning to a null-value pointer during serialization.
  1293. That is, passing an address of a pointer whose value is
  1294. X.I NULL 
  1295. to
  1296. X.I xdr_reference
  1297. when serialing data will most likely cause a memory fault and, on the UNIX
  1298. system, a core dump.
  1299. X.LP
  1300. X.I xdr_pointer 
  1301. correctly handles 
  1302. X.I NULL 
  1303. pointers.  For more information about its use, see 
  1304. \fILinked Lists\fP.
  1305. X.LP
  1306. X.I Exercise:
  1307. After reading the section on
  1308. \fILinked Lists\fP\, 
  1309. return here and extend example E so that
  1310. it can correctly deal with 
  1311. X.I NULL 
  1312. pointer values.
  1313. X.LP
  1314. X.I Exercise:
  1315. Using the
  1316. X.I xdr_union
  1317. X.I xdr_reference
  1318. and
  1319. X.I xdr_void
  1320. primitives, implement a generic pointer handling primitive
  1321. that implicitly deals with
  1322. X.I NULL 
  1323. pointers.  That is, implement
  1324. X.I xdr_pointer .
  1325. X.
  1326. X.NH 2
  1327. \&Non-filter Primitives
  1328. X.IX "XDR" "non-filter primitives"
  1329. X.IX "non-filter primitives"
  1330. X.LP
  1331. XDR streams can be manipulated with
  1332. the primitives discussed in this section.
  1333. X.DS
  1334. X.ft CW
  1335. u_int xdr_getpos(xdrs)
  1336.     XDR *xdrs;
  1337. X.sp.5
  1338. bool_t xdr_setpos(xdrs, pos)
  1339.     XDR *xdrs;
  1340.     u_int pos;
  1341. X.sp.5
  1342. xdr_destroy(xdrs)
  1343.     XDR *xdrs;
  1344. X.DE
  1345. The routine
  1346. X.I xdr_getpos
  1347. returns an unsigned integer
  1348. that describes the current position in the data stream.
  1349. Warning: In some XDR streams, the returned value of
  1350. X.I xdr_getpos
  1351. is meaningless;
  1352. the routine returns a \-1 in this case
  1353. (though \-1 should be a legitimate value).
  1354. X.LP
  1355. The routine
  1356. X.I xdr_setpos
  1357. sets a stream position to
  1358. X.I pos
  1359. Warning: In some XDR streams, setting a position is impossible;
  1360. in such cases,
  1361. X.I xdr_setpos
  1362. will return
  1363. X.I FALSE .
  1364. This routine will also fail if the requested position is out-of-bounds.
  1365. The definition of bounds varies from stream to stream.
  1366. X.LP
  1367. The
  1368. X.I xdr_destroy
  1369. primitive destroys the XDR stream.
  1370. Usage of the stream
  1371. after calling this routine is undefined.
  1372. X.
  1373. X.NH 2
  1374. \&XDR Operation Directions
  1375. X.IX "XDR operation directions"
  1376. X.IX "direction of XDR operations"
  1377. X.LP
  1378. At times you may wish to optimize XDR routines by taking
  1379. advantage of the direction of the operation \(em
  1380. X.I XDR_ENCODE
  1381. X.I XDR_DECODE
  1382. or
  1383. X.I XDR_FREE
  1384. The value
  1385. X.I xdrs->x_op
  1386. always contains the
  1387. direction of the XDR operation.
  1388. Programmers are not encouraged to take advantage of this information.
  1389. Therefore, no example is presented here.
  1390. However, an example in Section 7
  1391. demonstrates the usefulness of the
  1392. X.I xdrs->x_op
  1393. field.
  1394. X.
  1395. X.NH 2
  1396. \&XDR Stream Access
  1397. X.IX "XDR" "stream access"
  1398. X.IX "stream access"
  1399. X.LP
  1400. An XDR stream is obtained by calling the appropriate creation routine.
  1401. These creation routines take arguments that are tailored to the
  1402. specific properties of the stream.
  1403. X.LP
  1404. Streams currently exist for (de)serialization of data to or from
  1405. standard I/O
  1406. X.I FILE
  1407. streams, TCP/IP connections and UNIX files, and memory.
  1408. Section 5 documents the XDR object and how to make
  1409. new XDR streams when they are required.
  1410. X.
  1411. X.NH 3
  1412. \&Standard I/O Streams
  1413. X.IX "XDR" "standard I/O streams"
  1414. X.IX "standard I/O streams"
  1415. X.LP
  1416. XDR streams can be interfaced to standard I/O using the
  1417. X.I xdrstdio_create
  1418. routine as follows:
  1419. X.DS
  1420. X.ft CW
  1421. #include <stdio.h>
  1422. #include <rpc/rpc.h>    /* \fIxdr streams part of rpc\fP */
  1423. X.sp.5
  1424. void
  1425. xdrstdio_create(xdrs, fp, x_op)
  1426.     XDR *xdrs;
  1427.     FILE *fp;
  1428.     enum xdr_op x_op;
  1429. X.DE
  1430. The routine
  1431. X.I xdrstdio_create
  1432. initializes an XDR stream pointed to by
  1433. X.I xdrs
  1434. The XDR stream interfaces to the standard I/O library.
  1435. Parameter
  1436. X.I fp
  1437. is an open file, and
  1438. X.I x_op
  1439. is an XDR direction.
  1440. X.
  1441. X.NH 3
  1442. \&Memory Streams
  1443. X.IX "XDR" "memory streams"
  1444. X.IX "memory streams"
  1445. X.LP
  1446. Memory streams allow the streaming of data into or out of
  1447. a specified area of memory:
  1448. X.DS
  1449. X.ft CW
  1450. #include <rpc/rpc.h>
  1451. X.sp.5
  1452. void
  1453. xdrmem_create(xdrs, addr, len, x_op)
  1454.     XDR *xdrs;
  1455.     char *addr;
  1456.     u_int len;
  1457.     enum xdr_op x_op;
  1458. X.DE
  1459. The routine
  1460. X.I xdrmem_create
  1461. initializes an XDR stream in local memory.
  1462. The memory is pointed to by parameter
  1463. X.I addr
  1464. parameter
  1465. X.I len
  1466. is the length in bytes of the memory.
  1467. The parameters
  1468. X.I xdrs
  1469. and
  1470. X.I x_op
  1471. are identical to the corresponding parameters of
  1472. X.I xdrstdio_create
  1473. Currently, the UDP/IP implementation of RPC uses
  1474. X.I xdrmem_create
  1475. Complete call or result messages are built in memory before calling the
  1476. X.I sendto
  1477. system routine.
  1478. X.
  1479. X.NH 3
  1480. \&Record (TCP/IP) Streams
  1481. X.IX "XDR" "record (TCP/IP) streams"
  1482. X.IX "record (TCP/IP) streams"
  1483. X.LP
  1484. A record stream is an XDR stream built on top of
  1485. a record marking standard that is built on top of the
  1486. UNIX file or 4.2 BSD connection interface.
  1487. X.DS
  1488. X.ft CW
  1489. #include <rpc/rpc.h>    /* \fIxdr streams part of rpc\fP */
  1490. X.sp.5
  1491. xdrrec_create(xdrs,
  1492.   sendsize, recvsize, iohandle, readproc, writeproc)
  1493.     XDR *xdrs;
  1494.     u_int sendsize, recvsize;
  1495.     char *iohandle;
  1496.     int (*readproc)(), (*writeproc)();
  1497. X.DE
  1498. The routine
  1499. X.I xdrrec_create
  1500. provides an XDR stream interface that allows for a bidirectional,
  1501. arbitrarily long sequence of records.
  1502. The contents of the records are meant to be data in XDR form.
  1503. The stream's primary use is for interfacing RPC to TCP connections.
  1504. However, it can be used to stream data into or out of normal
  1505. UNIX files.
  1506. X.LP
  1507. The parameter
  1508. X.I xdrs
  1509. is similar to the corresponding parameter described above.
  1510. The stream does its own data buffering similar to that of standard I/O.
  1511. The parameters
  1512. X.I sendsize
  1513. and
  1514. X.I recvsize
  1515. determine the size in bytes of the output and input buffers, respectively;
  1516. if their values are zero (0), then predetermined defaults are used.
  1517. When a buffer needs to be filled or flushed, the routine
  1518. X.I readproc
  1519. or
  1520. X.I writeproc
  1521. is called, respectively.
  1522. The usage and behavior of these
  1523. routines are similar to the UNIX system calls
  1524. X.I read 
  1525. and
  1526. X.I write .
  1527. However,
  1528. the first parameter to each of these routines is the opaque parameter
  1529. X.I iohandle
  1530. The other two parameters
  1531. X.I buf
  1532. and
  1533. X.I nbytes
  1534. and the results
  1535. (byte count) are identical to the system routines.
  1536. If
  1537. X.I xxx 
  1538. is
  1539. X.I readproc
  1540. or
  1541. X.I writeproc
  1542. then it has the following form:
  1543. X.DS
  1544. X.ft CW
  1545. X.ft I
  1546. /*
  1547.  * returns the actual number of bytes transferred.
  1548.  * -1 is an error
  1549.  */
  1550. X.ft CW
  1551. int
  1552. xxx(iohandle, buf, len)
  1553.     char *iohandle;
  1554.     char *buf;
  1555.     int nbytes;
  1556. X.DE
  1557. The XDR stream provides means for delimiting records in the byte stream.
  1558. The implementation details of delimiting records in a stream
  1559. are discussed in appendix 1.
  1560. The primitives that are specific to record streams are as follows:
  1561. X.DS
  1562. X.ft CW
  1563. bool_t
  1564. xdrrec_endofrecord(xdrs, flushnow)
  1565.     XDR *xdrs;
  1566.     bool_t flushnow;
  1567. X.sp.5
  1568. bool_t
  1569. xdrrec_skiprecord(xdrs)
  1570.     XDR *xdrs;
  1571. X.sp.5
  1572. bool_t
  1573. xdrrec_eof(xdrs)
  1574.     XDR *xdrs;
  1575. X.DE
  1576. The routine
  1577. X.I xdrrec_endofrecord
  1578. causes the current outgoing data to be marked as a record.
  1579. If the parameter
  1580. X.I flushnow
  1581. is
  1582. X.I TRUE ,
  1583. then the stream's
  1584. X.I writeproc 
  1585. will be called; otherwise,
  1586. X.I writeproc 
  1587. will be called when the output buffer has been filled.
  1588. X.LP
  1589. The routine
  1590. X.I xdrrec_skiprecord
  1591. causes an input stream's position to be moved past
  1592. the current record boundary and onto the
  1593. beginning of the next record in the stream.
  1594. X.LP
  1595. If there is no more data in the stream's input buffer,
  1596. then the routine
  1597. X.I xdrrec_eof
  1598. returns
  1599. X.I TRUE .
  1600. That is not to say that there is no more data
  1601. in the underlying file descriptor.
  1602. X.
  1603. X.NH 2
  1604. \&XDR Stream Implementation
  1605. X.IX "XDR" "stream implementation"
  1606. X.IX "stream implementation in XDR"
  1607. X.LP
  1608. This section provides the abstract data types needed
  1609. to implement new instances of XDR streams.
  1610. X.
  1611. X.NH 3
  1612. \&The XDR Object
  1613. X.IX "XDR" "object"
  1614. X.IX "object"
  1615. X.LP
  1616. The following structure defines the interface to an XDR stream:
  1617. X.ie t .DS
  1618. X.el .DS L
  1619. X.ft CW
  1620. enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 };
  1621. X.sp.5
  1622. typedef struct {
  1623.     enum xdr_op x_op;            /* \fIoperation; fast added param\fP */
  1624.     struct xdr_ops {
  1625.         bool_t  (*x_getlong)();  /* \fIget long from stream\fP */
  1626.         bool_t  (*x_putlong)();  /* \fIput long to stream\fP */
  1627.         bool_t  (*x_getbytes)(); /* \fIget bytes from stream\fP */
  1628.         bool_t  (*x_putbytes)(); /* \fIput bytes to stream\fP */
  1629.         u_int   (*x_getpostn)(); /* \fIreturn stream offset\fP */
  1630.         bool_t  (*x_setpostn)(); /* \fIreposition offset\fP */
  1631.         caddr_t (*x_inline)();   /* \fIptr to buffered data\fP */
  1632.         VOID    (*x_destroy)();  /* \fIfree private area\fP */
  1633.     } *x_ops;
  1634.     caddr_t     x_public;        /* \fIusers' data\fP */
  1635.     caddr_t     x_private;       /* \fIpointer to private data\fP */
  1636.     caddr_t     x_base;          /* \fIprivate for position info\fP */
  1637.     int         x_handy;         /* \fIextra private word\fP */
  1638. } XDR;
  1639. X.DE
  1640. The
  1641. X.I x_op
  1642. field is the current operation being performed on the stream.
  1643. This field is important to the XDR primitives,
  1644. but should not affect a stream's implementation.
  1645. That is, a stream's implementation should not depend
  1646. on this value.
  1647. The fields
  1648. X.I x_private
  1649. X.I x_base
  1650. and
  1651. X.I x_handy
  1652. are private to the particular
  1653. stream's implementation.
  1654. The field
  1655. X.I x_public
  1656. is for the XDR client and should never be used by
  1657. the XDR stream implementations or the XDR primitives.
  1658. X.LP
  1659. Macros for accessing  operations
  1660. X.I x_getpostn
  1661. X.I x_setpostn
  1662. and
  1663. X.I x_destroy
  1664. were defined in Section 3.6.
  1665. The operation
  1666. X.I x_inline
  1667. takes two parameters:
  1668. an XDR *, and an unsigned integer, which is a byte count.
  1669. The routine returns a pointer to a piece of
  1670. the stream's internal buffer.
  1671. The caller can then use the buffer segment for any purpose.
  1672. From the stream's point of view, the bytes in the
  1673. buffer segment have been consumed or put.
  1674. The routine may return
  1675. X.I NULL 
  1676. if it cannot return a buffer segment of the requested size.
  1677. (The
  1678. X.I x_inline 
  1679. routine is for cycle squeezers.
  1680. Use of the resulting buffer is not data-portable.
  1681. Users are encouraged not to use this feature.) 
  1682. X.LP
  1683. The operations
  1684. X.I x_getbytes
  1685. and
  1686. X.I x_putbytes
  1687. blindly get and put sequences of bytes
  1688. from or to the underlying stream;
  1689. they return
  1690. X.I TRUE 
  1691. if they are successful, and
  1692. X.I FALSE 
  1693. otherwise.  The routines have identical parameters (replace
  1694. X.I xxx ):
  1695. X.DS
  1696. X.ft CW
  1697. bool_t
  1698. xxxbytes(xdrs, buf, bytecount)
  1699.     XDR *xdrs;
  1700.     char *buf;
  1701.     u_int bytecount;
  1702. X.DE
  1703. The operations
  1704. X.I x_getlong
  1705. and
  1706. X.I x_putlong
  1707. receive and put
  1708. long numbers from and to the data stream.
  1709. It is the responsibility of these routines
  1710. to translate the numbers between the machine representation
  1711. and the (standard) external representation.
  1712. The UNIX primitives
  1713. X.I htonl
  1714. and
  1715. X.I ntohl
  1716. can be helpful in accomplishing this.
  1717. Section 6 defines the standard representation of numbers.
  1718. The higher-level XDR implementation assumes that
  1719. signed and unsigned long integers contain the same number of bits,
  1720. and that nonnegative integers
  1721. have the same bit representations as unsigned integers.
  1722. The routines return
  1723. X.I TRUE
  1724. if they succeed, and
  1725. X.I FALSE 
  1726. otherwise.  They have identical parameters:
  1727. X.DS
  1728. X.ft CW
  1729. bool_t
  1730. xxxlong(xdrs, lp)
  1731.     XDR *xdrs;
  1732.     long *lp;
  1733. X.DE
  1734. Implementors of new XDR streams must make an XDR structure
  1735. (with new operation routines) available to clients,
  1736. using some kind of create routine.
  1737. X.
  1738. X.NH 1
  1739. \&Advanced Topics
  1740. X.IX XDR "advanced topics"
  1741. X.IX "advanced topics"
  1742. X.LP
  1743. This section describes techniques for passing data structures that
  1744. are not covered in the preceding sections.  Such structures include
  1745. linked lists (of arbitrary lengths).  Unlike the simpler examples
  1746. covered in the earlier sections, the following examples are written
  1747. using both the XDR C library routines and the XDR data description 
  1748. language.  The 
  1749. \fIeXternal Data Representation Standard\fP
  1750. chapter of this
  1751. X.I "Networking Programming" 
  1752. manual describes this language in complete detail. 
  1753. X.
  1754. X.NH 2
  1755. \&Linked Lists
  1756. X.IX "linked lists"
  1757. X.IX XDR "linked lists"
  1758. X.LP
  1759. The last example in the
  1760. \fIPointers\fP
  1761. section presented a C data structure and its associated XDR 
  1762. routines for a individual's gross assets and liabilities.  
  1763. The example is duplicated below:
  1764. X.ie t .DS
  1765. X.el .DS L
  1766. X.ft CW
  1767. struct gnumbers {
  1768.     long g_assets;
  1769.     long g_liabilities;
  1770. };
  1771. X.sp.5
  1772. bool_t
  1773. xdr_gnumbers(xdrs, gp)
  1774.     XDR *xdrs;
  1775.     struct gnumbers *gp;
  1776. {
  1777.     if (xdr_long(xdrs, &(gp->g_assets)))
  1778.         return(xdr_long(xdrs, &(gp->g_liabilities)));
  1779.     return(FALSE);
  1780. }
  1781. X.DE
  1782. X.LP
  1783. Now assume that we wish to implement a linked list of such information. 
  1784. A data structure could be constructed as follows:
  1785. X.ie t .DS
  1786. X.el .DS L
  1787. X.ft CW
  1788. struct gnumbers_node {
  1789.     struct gnumbers gn_numbers;
  1790.     struct gnumbers_node *gn_next;
  1791. };
  1792. X.sp .5
  1793. typedef struct gnumbers_node *gnumbers_list;
  1794. X.DE
  1795. X.LP
  1796. The head of the linked list can be thought of as the data object;
  1797. that is, the head is not merely a convenient shorthand for a
  1798. structure.  Similarly the 
  1799. X.I gn_next 
  1800. field is used to indicate whether or not the object has terminated.  
  1801. Unfortunately, if the object continues, the 
  1802. X.I gn_next 
  1803. field is also the address of where it continues. The link addresses 
  1804. carry no useful information when the object is serialized.
  1805. LP
  1806. The XDR data description of this linked list is described by the 
  1807. recursive declaration of 
  1808. X.I gnumbers_list :
  1809. X.ie t .DS
  1810. X.el .DS L
  1811. X.ft CW
  1812. struct gnumbers {
  1813.     int g_assets;
  1814.     int g_liabilities;
  1815. };
  1816. X.sp .5
  1817. struct gnumbers_node {
  1818.     gnumbers gn_numbers;
  1819.     gnumbers_list gn_next;
  1820. };
  1821. X.sp .5
  1822. union gnumbers_list switch (bool more_data) {
  1823. case TRUE:
  1824.     gnumbers_node node;
  1825. case FALSE:
  1826.     void;
  1827. };
  1828. X.DE
  1829. X.LP
  1830. In this description, the boolean indicates whether there is more data
  1831. following it. If the boolean is 
  1832. X.I FALSE ,
  1833. then it is the last data field of the structure. If it is 
  1834. X.I TRUE ,
  1835. then it is followed by a gnumbers structure and (recursively) by a 
  1836. X.I gnumbers_list .
  1837. Note that the C declaration has no boolean explicitly declared in it 
  1838. (though the 
  1839. X.I gn_next 
  1840. field implicitly carries the information), while the XDR data 
  1841. description has no pointer explicitly declared in it.
  1842. X.LP
  1843. Hints for writing the XDR routines for a 
  1844. X.I gnumbers_list 
  1845. follow easily from the XDR description above. Note how the primitive 
  1846. X.I xdr_pointer 
  1847. is used to implement the XDR union above.
  1848. X.ie t .DS
  1849. X.el .DS L
  1850. X.ft CW
  1851. bool_t
  1852. xdr_gnumbers_node(xdrs, gn)
  1853.     XDR *xdrs;
  1854.     gnumbers_node *gn;
  1855. {
  1856.     return(xdr_gnumbers(xdrs, &gn->gn_numbers) &&
  1857.         xdr_gnumbers_list(xdrs, &gp->gn_next));
  1858. }
  1859. X.sp .5
  1860. bool_t
  1861. xdr_gnumbers_list(xdrs, gnp)
  1862.     XDR *xdrs;
  1863.     gnumbers_list *gnp;
  1864. {
  1865.     return(xdr_pointer(xdrs, gnp, 
  1866.         sizeof(struct gnumbers_node), 
  1867.         xdr_gnumbers_node));
  1868. }
  1869. X.DE
  1870. X.LP
  1871. The unfortunate side effect of XDR'ing a list with these routines
  1872. is that the C stack grows linearly with respect to the number of
  1873. node in the list.  This is due to the recursion. The following
  1874. routine collapses the above two mutually recursive into a single,
  1875. non-recursive one.
  1876. X.ie t .DS
  1877. X.el .DS L
  1878. X.ft CW
  1879. bool_t
  1880. xdr_gnumbers_list(xdrs, gnp)
  1881.     XDR *xdrs;
  1882.     gnumbers_list *gnp;
  1883. {
  1884.     bool_t more_data;
  1885.     gnumbers_list *nextp;
  1886. X.sp .5
  1887.     for (;;) {
  1888.         more_data = (*gnp != NULL);
  1889.         if (!xdr_bool(xdrs, &more_data)) {
  1890.             return(FALSE);
  1891.         }
  1892.         if (! more_data) {
  1893.             break;
  1894.         }
  1895.         if (xdrs->x_op == XDR_FREE) {
  1896.             nextp = &(*gnp)->gn_next;
  1897.         }
  1898.         if (!xdr_reference(xdrs, gnp, 
  1899.             sizeof(struct gnumbers_node), xdr_gnumbers)) {
  1900.         
  1901.         return(FALSE);
  1902.         }
  1903.         gnp = (xdrs->x_op == XDR_FREE) ? 
  1904.             nextp : &(*gnp)->gn_next;
  1905.     }
  1906.     *gnp = NULL;
  1907.     return(TRUE);
  1908. }
  1909. X.DE
  1910. X.LP
  1911. The first task is to find out whether there is more data or not,
  1912. so that this boolean information can be serialized. Notice that
  1913. this statement is unnecessary in the 
  1914. X.I XDR_DECODE 
  1915. case, since the value of more_data is not known until we 
  1916. deserialize it in the next statement.
  1917. X.LP
  1918. The next statement XDR's the more_data field of the XDR union. 
  1919. Then if there is truly no more data, we set this last pointer to 
  1920. X.I NULL 
  1921. to indicate the end of the list, and return 
  1922. X.I TRUE 
  1923. because we are done. Note that setting the pointer to 
  1924. X.I NULL 
  1925. is only important in the 
  1926. X.I XDR_DECODE 
  1927. case, since it is already 
  1928. X.I NULL 
  1929. in the 
  1930. X.I XDR_ENCODE 
  1931. and 
  1932. XDR_FREE 
  1933. cases.
  1934. X.LP
  1935. Next, if the direction is 
  1936. X.I XDR_FREE ,
  1937. the value of 
  1938. X.I nextp 
  1939. is set to indicate the location of the next pointer in the list. 
  1940. We do this now because we need to dereference gnp to find the 
  1941. location of the next item in the list, and after the next 
  1942. statement the pointer 
  1943. X.I gnp 
  1944. will be freed up and no longer valid.  We can't do this for all 
  1945. directions though, because in the 
  1946. X.I XDR_DECODE 
  1947. direction the value of 
  1948. X.I gnp 
  1949. won't be set until the next statement.
  1950. X.LP
  1951. Next, we XDR the data in the node using the primitive 
  1952. X.I xdr_reference .
  1953. X.I xdr_reference 
  1954. is like 
  1955. X.I xdr_pointer 
  1956. which we used before, but it does not
  1957. send over the boolean indicating whether there is more data. 
  1958. We use it instead of 
  1959. X.I xdr_pointer 
  1960. because we have already XDR'd this information ourselves. Notice 
  1961. that the xdr routine passed is not the same type as an element 
  1962. in the list. The routine passed is 
  1963. X.I xdr_gnumbers ,
  1964. for XDR'ing gnumbers, but each element in the list is actually of 
  1965. type 
  1966. X.I gnumbers_node .
  1967. We don't pass 
  1968. X.I xdr_gnumbers_node 
  1969. because it is recursive, and instead use 
  1970. X.I xdr_gnumbers 
  1971. which XDR's all of the non-recursive part.  Note that this trick 
  1972. will work only if the 
  1973. X.I gn_numbers 
  1974. field is the first item in each element, so that their addresses 
  1975. are identical when passed to 
  1976. X.I xdr_reference .
  1977. X.LP
  1978. Finally, we update 
  1979. X.I gnp 
  1980. to point to the next item in the list. If the direction is 
  1981. X.I XDR_FREE ,
  1982. we set it to the previously saved value, otherwise we can 
  1983. dereference 
  1984. X.I gnp 
  1985. to get the proper value.  Though harder to understand than the 
  1986. recursive version, this non-recursive routine will never cause 
  1987. the C stack to blow up. It will also run more efficiently since 
  1988. a lot of procedure call overhead has been removed. Most lists 
  1989. are small though (in the hundreds of items or less) and the 
  1990. recursive version should be sufficient for them.
  1991. Funky_Stuff
  1992. cd ..
  1993. echo more files to follow
  1994. exit
  1995.